package  menu
{
	import com.*;
	import com.Logging.ClientAction;
	import com.Logging.ClientActionType;
	import ui.popup.GCInventoryPopup;
	
	import data.*;
	
	import events.GCEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import ui.button.GCButton;
	import ui.list.GCItemScrollList;
	import ui.list.GCListUI;
	import ui.textfield.GCStylizedText;
	
	/**
	 * Menu to display the inventory of player, either in
	 * their ship or one of their city warehouses.
	 * @author Jason Won
	 */
	public class GCInventoryMenu extends Sprite implements GCSuperMenu
	{
		// Inventory List
		private var _inventoryListDisplay:GCItemScrollList;
		
		// Labels
		private var _shipInventoryLabel:GCStylizedText;
		private var _inventoryDescriptionLabel:GCStylizedText;
		
		// Menu UI Elements
		private var _descriptionText:TextField;
		private var _descriptionBox:Sprite;
		private var _removeIcon:GCButton;
		private var _exitButton:GCButton;
		
		// Popup to discard items
		private var _discardPopup:GCInventoryPopup;
		
		// Keep track of which item was highlighted
		private var _selectedItem:GCItem;
		private var _selectedListUI:GCListUI;
		
		public function GCInventoryMenu() 
		{
			// Menu Background
			this.graphics.beginFill(0x0066cc);
			this.graphics.drawRect(0, 0, GCMainDisplay.WIDTH, GCMainDisplay.HEIGHT);
			this.graphics.endFill();
			
			// Create the inventory list			
			_inventoryListDisplay = new GCItemScrollList(300, 400);					
			
			// Create Labels
			_shipInventoryLabel = new GCStylizedText();
			_inventoryDescriptionLabel = new GCStylizedText();
			
			// Create the UI Elements
			_descriptionText = new TextField();
			_descriptionBox = new Sprite();
			_removeIcon = new GCButton(onClickRemove);
			_exitButton = new GCButton(onExitMenu);
			
			// Create the popup
			_discardPopup = new GCInventoryPopup();
			
			init();			
		}
		
		private function init():void
		{
			_discardPopup.addEventListener(GCEvent.DISCARD_CONFIRM_EVENT, onConfirmDiscard);
			_discardPopup.addEventListener(GCEvent.CANCEL_EVENT, onCancelDiscard);
			
			// Define the list component
			_inventoryListDisplay.x = 50;
			_inventoryListDisplay.y = 30;
			_inventoryListDisplay.addEventListener(GCEvent.LIST_ITEM_SELECTED_EVENT, onItemSelect);
			
			// Define the UI Components
			_descriptionBox.x = 400;
			_descriptionBox.y = 30;
			_descriptionBox.graphics.beginFill(0x222222);
			_descriptionBox.graphics.drawRect(0, 0, 300, 400);
			_descriptionBox.graphics.endFill();
			
			_descriptionText.textColor = 0xffffff;
			_descriptionText.width = _descriptionBox.width;
			_descriptionText.height = _descriptionBox.height;
			_descriptionText.wordWrap = true;
			_descriptionBox.addChild(_descriptionText);			
			
			_exitButton.loadGraphic(GCResources.plate, 100, 30);
			_exitButton.loadText("Exit Inventory");
			_exitButton.x = this.width / 2;
			_exitButton.y = this.height - 70;
			
			_removeIcon.loadGraphic(GCResources.plate, 100, 30);
			_removeIcon.loadText("Discard Item");
			_removeIcon.x = this.width / 3;
			_removeIcon.y = this.height - 70;
					
			_shipInventoryLabel.setBgStyle(GCStylizedText.STYLE_ONE, _inventoryListDisplay.width, 30);
			_shipInventoryLabel.x = _inventoryListDisplay.x;
			_shipInventoryLabel.text = "Items on the Ship";
			_inventoryDescriptionLabel.setBgStyle(GCStylizedText.STYLE_ONE, _descriptionBox.width, 30);
			_inventoryDescriptionLabel.x = _descriptionBox.x;
			_inventoryDescriptionLabel.text = "Item Description";
			
			
			// Add all of the elements
			this.addChild(_shipInventoryLabel);
			this.addChild(_inventoryDescriptionLabel);
			this.addChild(_inventoryListDisplay);
			this.addChild(_descriptionBox);
			this.addChild(_exitButton);
			this.addChild(_removeIcon);
		}
		
		/**
		 * Here we will initialize the contents of the list.
		 */
		public function openMenu(shipInventory:GCInventory):void
		{
			_inventoryListDisplay.loadInventory(shipInventory);
		}
		
		public function closeMenu():void
		{
			_inventoryListDisplay.removeAll();
			_descriptionText.text = "";
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Updates the current lists on display
		 */
		public function refresh():void
		{
			_inventoryListDisplay.redraw();
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		private function onExitMenu(event:MouseEvent):void
		{
			dispatchEvent(new GCEvent(GCEvent.EXIT_MENU_EVENT));
		}
		
		
		private function onClickRemove(event:Event):void
		{
			_selectedListUI = _inventoryListDisplay.selected
			if (_selectedListUI != null)
			{
				_selectedItem = _selectedListUI.reference;
				_discardPopup.initializePopup(_selectedItem.quantity, GCItemData.getNameFromId(_selectedItem.itemId), GCInventoryPopup.ACTION_DISCARDING);
				GCPlayState.disableScreen(_discardPopup);
			}
		}
		
		/**
		 * Fills the description box when an item is selected
		 * @param	event
		 */
		private function onItemSelect(event:GCEvent):void
		{
			var item:GCItem = _inventoryListDisplay.selected.reference;
			var itemName:String = GCItemData.getNameFromId(item.itemId);
			var description:String = GCItemData.getItemDescriptionFromId(item.itemId);
			
			if (GCConstants.LOGGING_DATA)
			{
				var o:Object = new Object();
				o["itemId"] = item.itemId;
				GCPlayState.logger.LogAction(new ClientAction(ClientActionType.VIEW_INV_ITEM, o));
			}
			
			_descriptionText.text = "Name: " + itemName + "\n\n";
			_descriptionText.appendText("Description: " + description + "\n");
		}
		
		private function onConfirmDiscard(event:Event):void
		{
			
			var amountToDiscard:uint = _discardPopup.getQuantity();
			var amountLeftOver:uint = _selectedItem.quantity - amountToDiscard;
			
			if (GCConstants.DEBUG_MODE)
			{
				onPerformDiscard(amountToDiscard, amountLeftOver);
			}
			else
			{
				//DATABASE CALL- discarding an item from the list
				GCUtil.updateItemQuantity(_selectedItem.invItemId, amountLeftOver, function(code:uint, data:Object):void
				{
					trace("DATABASE-- removed an item from the ship's inventory");
					onPerformDiscard(amountToDiscard, amountLeftOver);
				});
			}
			
			function onPerformDiscard(amountToDiscard:uint, amountLeftOver:uint):void
			{
				if (amountLeftOver > 0)
				{
					GCPlayState.getPlayerInventory().removeFromInventory(_selectedItem.itemId, amountToDiscard);
					
					// Reload the inventory
					_inventoryListDisplay.loadInventory(GCPlayState.getPlayerInventory());
				}
				else
				{
					_inventoryListDisplay.removeItem(_selectedItem);
					_descriptionText.text = "";
					_selectedItem = null;
					_selectedListUI = null;
				}
				
				onCancelDiscard(event);
			}
		}
		
		private function onCancelDiscard(event:Event):void
		{
			_discardPopup.clearPopup();
			GCPlayState.enableScreen();
		}
	}
}